package de.app.badresden;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;

public class LoadImageFromUrl {

	public static Bitmap load(String url) {

		BitmapFactory.Options bmOptions;
		bmOptions = new BitmapFactory.Options();
	    bmOptions.inSampleSize = 1;  

	    Bitmap bitmap = null;
	    InputStream in = null;       
	    
	       try {
	           in = openHttpConnection(url);
	           bitmap = BitmapFactory.decodeStream(in, null, bmOptions);
	           in.close();
	       } catch (IOException e) {
	    	   Log.d("ImageFromUrlLoader", e.getMessage());
	       }
	       return bitmap;            
	}
	
	public static Bitmap loadFromStream(InputStream stream) {

		BitmapFactory.Options bmOptions;
		bmOptions = new BitmapFactory.Options();
	    bmOptions.inSampleSize = 1;  
    
	    Bitmap bitmap = null;
	     
	    try {  
	    	bitmap = BitmapFactory.decodeStream(stream, null, bmOptions);	           
	    	stream.close();
	     	   
	    } catch (IOException e) {    	   
	    	Log.d("ImageFromUrlLoader", e.getMessage());	      	   
	    }      
	    return bitmap;            
	}
		
	public static InputStream openHttpConnection(String strURL) throws IOException {
		 InputStream inputStream = null;
		 URL url = new URL(strURL);
		 URLConnection conn = url.openConnection();

		 try {		 
			 HttpURLConnection httpConn = (HttpURLConnection)conn;		 
			 httpConn.setRequestMethod("GET");			 
			 httpConn.connect();
	 
			 if (httpConn.getResponseCode() == HttpURLConnection.HTTP_OK) 		   			
				 inputStream = httpConn.getInputStream();			
		 }
		 catch (Exception e) {
			 Log.d("ImageFromUrlLoader", e.getMessage());	
		 }
		 return inputStream;		
	}
}
